/*
 * @Descripttion: 
 * @version: 
 * @Author: Indolphin
 * @Date: 2021-02-28 21:12:46
 * @LastEditors: Indolphin
 * @LastEditTime: 2021-03-14 14:47:37
 */
/**
 * redux reducer
 * 
 * @author Indolphin
 */
import { combineReducers } from 'redux';
import * as models from '../interface';
import * as types from './types';
import storage from '../utils/storeage';
/**
 * 用户初始化数据
 */
const { username } = storage.getUserInfo();
const initialUser = {
    username
};

/**
 * 用户操作reducer
 * 
 * @param state 状态数据
 * @param action redux action
 * @returns 
 */
function user(state = initialUser, action: types.UserActionType): models.User {
    // 解构action
    const { type, payload } = action; 
    switch(type) {
        case types.UPDATE_USER: 
            console.log("action = ", action);
            return {
                ...state,
                username: payload.username
            };
        default:
            return state
    }
}

const initialMenus: { menus: models.Menu[] } = {
    menus: []
}
function menus(state = initialMenus, action: types.QueryMenusType): {  menus: models.Menu[] } {
    const { type, payload } = action;
    switch(type) {
        case types.QUERY_MENUS:
            return {
                ...state,
                menus: payload.menus
            };
        default:
            return state
    }
}


/**
 * 合并所有reducers
 */
const reducers = combineReducers({
    user,
    menus
});
export type reducerType = ReturnType<typeof reducers>;
export default reducers;